package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dish")
@RestController
@Slf4j
public class DishController {
 @Autowired
 private DishService dishService;

 @PostMapping
 @CacheEvict(value = "dish",allEntries = true)
 public R<String>save(@RequestBody DishDto dishDto){
  dishService.save1(dishDto);
  return R.success("添加成功");
 }
 @GetMapping("/page")

 public R page (Integer page,Integer pageSize,String name){

  Page<DishDto> dishDtoPage = dishService.pageByParams(page, pageSize, name);

  return  R.success(dishDtoPage);
 }

 @GetMapping("/{id}")
 public R<DishDto> get(@PathVariable Long id){
  DishDto dishDto = dishService.getByIdWithFlavor(id);
  return R.success(dishDto);
 }
 @PutMapping
 @CacheEvict(value = "dish",allEntries = true)
 public R<String> update(@RequestBody DishDto dishDto){
  log.info(dishDto.toString());
  dishService.updateWithFlavor(dishDto);
  return R.success("修改菜品成功");
 }
 @DeleteMapping()
 @CacheEvict(value = "dish",allEntries = true)
 public R<String> delete(@RequestParam List<Long> ids){
  R<String> delete = dishService.delete1(ids);
  return delete;
 }
 //批量起售
 @PostMapping("/status/{status}")
 @CacheEvict(value = "dish",allEntries =true)
 public R<String>upDateStatus1(@PathVariable Integer status,@RequestParam ("ids")List<Long> ids){
  R<String> stringR = dishService.upDateStatus(status,ids);
  return stringR;
 }
 //新建套餐中添加菜品
 @GetMapping("/list")
 @Cacheable(value = "dish",key = "#dish.categoryId"+'-'+"#dish.status")
 public R<List<DishDto>>getDish(Dish dish){
  log.info("{dish}",dish);
  R<List<DishDto>> dishList = dishService.getDish(dish);
  return dishList;
 }
}
